import axios from 'axios'
// development 开发环境 production 生产环境
const isDev = process.env.NODE_ENV === 'development'

// baseUrl 会自动加到所有请求之前
const request = axios.create({
  // http://121.89.205.189/api/banner/list ==> /banner/list
  baseURL: isDev ? 'http://121.89.205.189/api' : 'http://121.89.205.189/api',
  timeout: 6000
})

// 拦截器
request.interceptors.request.use(config => {
  config.headers.common.token = localStorage.getItem('token')
  return config
}, err => Promise.reject(err))

request.interceptors.response.use(response => {
  if (response.data.code === '10119') {
    this.$router.push('/login')
  }
  return response
}, err => Promise.reject(err))

export default request
